package com.example.workrecord.util.dingtalkopenapi.utils;

import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.example.workrecord.entity.CorpUserDetail;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
  *@ClassName: JwtUtils
  *@Description JwtUtils
  *@Author Mr.Bu
  *@Date 2020/3/4 9:58
  *@Version: 1.0
  */
public class JwtUtils {


    private static final String SECRET = "XX#$%()(#*!()!KL<><MQLMNQNQJQK sdfkjsdrow32234545fdf>?N<:{LWPW";

    private static final String EXP = "exp";

    private static final String PAYLOAD = "payload";

    /**
     * get jwt String of object
     *
     * @param object the POJO object
     * @param maxAge the milliseconds of life time
     * @return the jwt token
     */
    public static <T> String sign(T object, long maxAge) {
        try {
            final JWTSigner signer = new JWTSigner(SECRET);
            final Map<String, Object> claims = new HashMap<String, Object>();
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = mapper.writeValueAsString(object);
            claims.put(PAYLOAD, jsonString);
            claims.put(EXP, System.currentTimeMillis() + maxAge);
            return signer.sign(claims);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * get the object of jwt if not expired
     *
     * @param jwt
     * @return POJO object
     */
    public static <T> T unsign(String jwt, Class<T> classT) {
        final JWTVerifier verifier = new JWTVerifier(SECRET);
        try {
            final Map<String, Object> claims = verifier.verify(jwt);
            if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) {
                long exp = (Long) claims.get(EXP);
                long currentTimeMillis = System.currentTimeMillis();
                if (exp > currentTimeMillis) {
                    String json = (String) claims.get(PAYLOAD);
                    ObjectMapper objectMapper = new ObjectMapper();
                    return objectMapper.readValue(json, classT);
                }
            }
            System.out.println("签名解析失败，请检查！");
            return null;
        } catch (Exception e) {
            System.out.println("签名解析失败，请检查！"+e.getMessage());
            return null;
        }
    }


    public static void main(String[] args){
        Map map=new HashMap();
        map.put("11",11);
        map.put("22",22);
        String str=sign(map,30L * 24L * 3600L * 1000L);
        System.out.println(str);
        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m51bGwsXCJvcmRlckluRGVwdHNcIjpudWxsLFwib3JnRW1haWxcIjpudWxsLFwicG9zaXRpb25cIjpudWxsLFwicmVtYXJrXCI6bnVsbCxcInJvbGVzXCI6bnVsbCxcInN0YXRlQ29kZVwiOm51bGwsXCJ0ZWxcIjpudWxsLFwidW5pb25pZFwiOm51bGwsXCJ1c2VyaWRcIjpcIjEwMzYxMjQzMTczNzY1OTAxOFwiLFwid29ya1BsYWNlXCI6bnVsbCxcImNvcnBJZFwiOlwiZGluZzI4ZDFhNWQyMjdjMDJiNDgzNWMyZjQ2NTdlYjYzNzhmXCIsXCJwZXJtaXNzaW9uU2V0XCI6W1wiMjM5MTI5MDU5XCIsXCIzNDg2NDE2NjBcIixcIjM0ODcwMzAxNlwiLFwiNTQwNzExNTQ4XCIsXCIzNDg2NDM1NzNcIixcIjIzOTEyOTA1OFwiLFwiMzQ4NTM5ODYyXCIsXCIyMzkxMjkwNTdcIixcIjIzOTEyOTA2OFwiLFwiMzUxMzgxODI5XCIsXCI1ODkxMDkwOTRcIixcIjM0ODY3NTE3MFwiLFwiMjM5NDkyNjQ2XCIsXCIzNDg2ODIyMzlcIixcIjI1NTIxOTIwOVwiLFwiMjM5MTI5MDYzXCIsXCIzNDg1ODQ0MDhcIixcIjM0ODYzNjc3MlwiLFwiMjM5MTI5MDY1XCIsXCIyMzkxMjkwNjRcIixcIjIzOTMyMTIwOFwiLFwiNTI2MjEyMDkxXCIsXCIyMzkxMjkwNjFcIixcIjIzOTEyOTA2MFwiXSxcInRva2VuXCI6bnVsbCxcInN1Y2Nlc3NcIjp0cnVlfSJ9.vyaalkzaqOuAliIOlQiOEmpGLP6R3_aXb8c6lN2NorI";
        Map result=unsign(str,Map.class);
        System.out.println(result);
        CorpUserDetail userDetail=unsign(string,CorpUserDetail.class);
        System.out.println(userDetail);

    }
}